--稳虑言权
local wenlvyanquan = fk.CreateSkill {
    name = "mcdh_wenlvyanquan",
    attached_skill_name = "mcdh_wenlvyanquan_other&"
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

wenlvyanquan:addEffect('active',{
    prompt = "#mcdh_wenlvyanquan-active",
    mute = true,
    on_acquire =  function(self,player, is_start)
        local room = player.room
        for _, p in ipairs(room:getOtherPlayers(player)) do
            room:handleAddLoseSkills(p, "mcdh_wenlvyanquan_other&", nil, false, true)
        end
    end,
    on_lose = function (self, player)
        local room = player.room
        if table.every(room.alive_players, function(p) return not p:hasSkill(self, true) or p == player end) then
            for _, p in ipairs(room.alive_players) do
                room:handleAddLoseSkills(p, "-mcdh_wenlvyanquan_other&", nil, false, true)
            end
        end
    end,
    can_use = function(self, player)
        local skill_name = self.name:gsub("_other&", "")
        return table.find(
                Fk:currentRoom().alive_players,
                function(p)
                    return p:hasSkill(skill_name)
                            and p:usedSkillTimes(skill_name, Player.HistoryPhase) < 1
                end
        )
    end,
    card_num = 0,
    card_filter = Util.FalseFunc,
    target_num = 1,
    target_filter = function(self, player,to_select, selected, selected_cards)
        local target = to_select
        return #selected == 0 and target and target:hasSkill(self.name)
                and target:usedSkillTimes(self.name, Player.HistoryPhase) < 1
    end,
    on_use = function(self, room, effect)
        local player = effect.from
        local target = effect.tos[1]
        if target ~= player then
            target:setSkillUseHistory(self.name, 1, Player.HistoryPhase)
        end
        room:notifySkillInvoked(target, "mcdh_wenlvyanquan")
        target:broadcastSkillInvoke("mcdh_wenlvyanquan")
        room:doIndicate(effect.tos[1], { effect.from })

        local cards = {}
        room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function(e)
            for _, move in ipairs(e.data) do
                if move.toArea == Card.DiscardPile then
                    for _, info in ipairs(move.moveInfo) do
                        if room:getCardArea(info.cardId) == Card.DiscardPile then
                            table.insert(cards, info.cardId)
                        end
                    end
                end
            end
            return false
        end, Player.HistoryPhase)
        if #cards > 0 then
            local cids, _ =room:askforChooseCardsAndChoice(target,{
                cards = cards,
                choice = { "ok" },
                skill_name = self.name,
                prompt = "#mcdh_wenlvyanquan-choice",
                min_num = 1,
                max_num = 1,
                all_cards = cards
            })
            if #cids > 0 then
                room:moveCardTo(cids, Card.DrawPile, nil, fk.ReasonPut, self.name, nil, true, player)
                room:sendLog({
                    type = "#mcdh_wenlvyanquan-log",
                    from = player.id,
                    card = cids
                })
            end
        else
            room:sendLog({
                type = "#mcdh_wenlvyanquan-fail"
            })
        end
    end,
})

Fk:loadTranslationTable {
    ["mcdh_wenlvyanquan"] = "稳中",
    [":mcdh_wenlvyanquan"] = "『稳虑言权』<br>每名角色出牌阶段限一次，其可以令你从弃牌堆中选择一张本阶段内进入的牌，将之置于牌堆顶。",
    ["mcdh_wenlvyanquan_other&"] = "稳中",
    [":mcdh_wenlvyanquan_other&"] = "出牌阶段限一次，你可以令麦基将本阶段进入弃牌堆的一张牌置于牌堆顶。",
    ["#mcdh_wenlvyanquan-active"] = "发动一名角色的〖稳虑言权〗",
    ["#mcdh_wenlvyanquan-choice"] = "稳虑言权:从弃牌堆中选择要放到牌堆顶的牌",
    ["#mcdh_wenlvyanquan-log"] = "%from 将 %card 放到了牌堆顶",
    ["#mcdh_wenlvyanquan-fail"] = "但是什么也没有发生",
}

return wenlvyanquan